package com.tiny.climber;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import com.tiny.core.game.GameObject;
import com.tiny.core.game.util.MovimentCalculator;
import com.tiny.core.resources.ImageManager;
import com.tiny.core.resources.SpriteAnimation;
import com.tiny.core.resources.utils.ImageUtils;

public class Ptero extends GameObject {

    private SpriteAnimation ptero;
    private BufferedImage clock;
    private int clockX;
    private int clockY;
    private ClimbGame game;
    private MovimentCalculator calc;
    private boolean right;
    private boolean goAway;
    private float velocity;
    

    public Ptero(int x, int y, boolean right, ClimbGame game, ImageManager images, MovimentCalculator calc) {
        super(x, y, 131, 84);
        ptero = ImageUtils.loadSpriteAnimation(images.loadImage("img/hector.png"), 7);
        clock = images.loadImage("img/clock.png");
        ptero.setSpeed(22);
        this.right = right;
        this.game = game;
        this.calc = calc;
        goAway = false;
        velocity = 150;
    }

    @Override
    public void render(Graphics2D g) {
        g.drawImage(ptero.getImage(), null, (int) pos.getX(), (int) pos.getY());
        if (!goAway) {
            g.drawImage(clock, null, clockX, clockY);
        }
    }

    @Override
    public void update(int currentTick) {
        ptero.update(currentTick);
        ptero.setFliped(!right);
        if (getCollingAbove() instanceof TinyClimber || getCollingBelow() instanceof TinyClimber
                || getCollingRight() instanceof TinyClimber || getCollingLeft() instanceof TinyClimber) {
            goAway = true;
            velocity = 800;
            game.slowLava();
        }
        if (right) {
            getPos().x += calc.calculate(velocity);
        } else {
            getPos().x -= calc.calculate(velocity);
        }
        if (right) {
            clockX = (int) pos.x + 20;
        } else {
            clockX = (int) pos.x + 80;
        }
        clockY = (int) (pos.y + pos.height);

        if (!goAway) {
            if (pos.x <= 0) {
                right = true;
            }
            if (pos.x + pos.width >= game.getGameWidth()) {
                right = false;
            }
        } else {
            if (pos.x <= 0 || pos.x + pos.width >= game.getGameWidth()) {
                setEnergy(0);
                game.removePtero();
            }
        }
    }
}
